#ifndef COMPUTEROTATIONMATRIXOFEULAR_H
#define COMPUTEROTATIONMATRIXOFEULAR_H

#include "geometry/vertex3D.h"
#include "geometry/math3d.h"

#include "rotatebothaxises.h"
#include <QDebug>
#include "QtGui/QOpenGLFunctions"

/** 以欧拉角的形式，返回一个物体的旋转矩阵 */
GLfloat *computeRotationMatrixOfEular(
        double angleXInRadian, double angleYInRadian, double angleZInRadian) {
    GLfloat *rm = new GLfloat[16];

    Vertex3D *pO = new Vertex3D(0, 0, 0);
    Vertex3D *pX = new Vertex3D(1, 0, 0);
    Vertex3D *pY = new Vertex3D(0, 1, 0);
    Vertex3D *pZ = new Vertex3D(0, 0, 1);

    //dirX、dirY、dirZ分别代表三个不同的方向
    Vector3D *dirX = Math3D::direction(pO, pX);
    Vector3D *dirY = Math3D::direction(pO, pY);
    Vector3D *dirZ = Math3D::direction(pO, pZ);

    //将y、z绕x轴旋转
    QList<Vector3D*> *dirYZArray = new QList<Vector3D*>();
    dirYZArray->push_back(dirY); dirYZArray->push_back(dirZ);
    QList<Vector3D*> *dirYZ = rotateBothAxises(angleXInRadian, dirX, dirYZArray);
    // @ts-ignore
    dirY = dirYZ->at(0); dirZ = dirYZ->at(1);

    //将x、z绕y轴旋转
    QList<Vector3D*> *dirXZArray = new QList<Vector3D*>();
    dirXZArray->push_back(dirX); dirXZArray->push_back(dirZ);
    QList<Vector3D*> *dirXZ = rotateBothAxises(angleYInRadian, dirY, dirXZArray);
    // @ts-ignore
    dirX = dirXZ->at(0); dirZ = dirXZ->at(1);

    //将x、y绕z轴旋转
    QList<Vector3D*> *dirXYArray = new QList<Vector3D*>();
    dirXYArray->push_back(dirX); dirXYArray->push_back(dirY);
    QList<Vector3D*> *dirXY = rotateBothAxises(angleZInRadian, dirZ, dirXYArray);
    // @ts-ignore
    dirX = dirXY->at(0); dirY = dirXY->at(1);

    //qDebug() << "生命可以老去";

    rm[0]  = dirX->getDeltaX(); rm[1]  = dirX->getDeltaY(); rm[2]  = dirX->getDeltaZ(); rm[3]  = 0;
    rm[4]  = dirY->getDeltaX(); rm[5]  = dirY->getDeltaY(); rm[6]  = dirY->getDeltaZ(); rm[7]  = 0;
    rm[8]  = dirZ->getDeltaX(); rm[9]  = dirZ->getDeltaY(); rm[10] = dirZ->getDeltaZ(); rm[11] = 0;
    rm[12] = 0;                 rm[13] = 0;                 rm[14] = 0;                 rm[15] = 1;


    delete pO; delete pX; delete pY; delete pZ;
    delete dirX; delete dirY; delete dirZ;
    delete dirXYArray; delete dirXZArray; delete dirYZArray;

    return rm;
}

#endif // SINGLEFUNCTIONTEST_H
